package com.yangrd.airship.generator.page.base;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.yangrd.airship.generator.support.BasePageGeneratorConst;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author yangrd
 * @date 2021/11/8
 */
@Getter
@Setter
@JsonInclude(JsonInclude.Include.NON_NULL)
public class AbstractMetaInfo<T> implements Serializable {

    private Map<String, Object> meta;

    public T initMeta(String val) {
        return putMeta("default", val);
    }

    public T putMeta(String key, Object val) {
        if (Objects.isNull(meta)) {
            this.meta = new HashMap<>(8);
        }
        meta.put(key, val);
        return self();
    }

    public T putMeta(BasePageGeneratorConst defineConst, Object val) {
        return putMeta(defineConst.id(), val);
    }

    public T putMeta(Boolean canPut, BasePageGeneratorConst defineConst, Object val) {
        return canPut ? putMeta(defineConst.id(), val) : self();
    }

    protected T self() {
        return (T) this;
    }
}
